
package com.bird.pro.service.impl;


import java.util.List;


import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bird.pro.Page;
import com.bird.pro.Params;
import com.bird.pro.mapper.BirdAdminMapper;
import com.bird.pro.pojo.BirdAdmin;
import com.bird.pro.pojo.BirdAdminExample;
import com.bird.pro.pojo.BirdAdminExample.Criteria;
import com.bird.pro.service.IAdminService;

@Service
@Transactional
public class AdminServiceImpl implements IAdminService {

	@Autowired
	private BirdAdminMapper adminMapper;
	@Override
	public List<BirdAdmin> queryAdmins() {
		BirdAdminExample example = new BirdAdminExample();
		return adminMapper.selectByExample(example);
	}

	@Override
	public List<BirdAdmin> queryAdmins(String adminName, String adminPhone, String account) {
		BirdAdminExample example = new BirdAdminExample();
		Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(adminName)) {
			criteria.andAdminNameEqualTo(adminName);
		}
		if (StringUtils.isNotBlank(adminPhone)) {
			criteria.andAdminPhoneEqualTo(adminPhone);
		}
		if (StringUtils.isNotBlank(account)) {
			criteria.andAdminAccountEqualTo(account);
		}
		example.setOrderByClause("create_time DESC");
		return adminMapper.selectByExample(example);
	}

	@Override
	public BirdAdmin getAdminById(Integer id) {
		if (id != null) {
			return adminMapper.selectByPrimaryKey(id);
		}
		return null;
	}

	@Override
	public int saveAdmin(BirdAdmin admin) {
		if (StringUtils.isNotBlank(admin.getAdminAccount()) && StringUtils.isNotBlank(admin.getAdminPhone())) {
			return adminMapper.insertSelective(admin);
		}
		return 0;
	}

	@Override
	public int updateAdmin(BirdAdmin admin) {
		if (admin.getId() != null) {
			return adminMapper.updateByPrimaryKeySelective(admin);
		}
		return 0;
	}

	@Override
	public int deleteAdmin(Integer id) {
		if (id != null) {
			return adminMapper.deleteByPrimaryKey(id);
		}
		return 0;
	}

	@Override
	public Page queryByParams(Params params, Integer roleId) {
		BirdAdminExample example = new BirdAdminExample();
		Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(params.getName())) {
			criteria.andAdminNameLike(params.getName());
		}
		if (StringUtils.isNotBlank(params.getProvince())) {
			criteria.andAdminProvinceEqualTo(params.getProvince());
		}
		if (StringUtils.isNotBlank(params.getCity())) {
			criteria.andAdminCityEqualTo(params.getCity());
		}
		if (StringUtils.isNotBlank(params.getCounty())) {
			criteria.andAdminCountyEqualTo(params.getCounty());
		}
		if (roleId != null) {
			criteria.andRoleIdEqualTo(roleId);
			criteria.andAdminStateEqualTo("1");
			List<BirdAdmin> admins = adminMapper.selectByExample(example);
			Page page = new Page();
			page.setData(admins);
			return page;
		}
		return null;
	}

}
